package com.nixsolutions.controller;


import com.nixsolutions.bean.User;
import com.nixsolutions.validator.UserFormValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author yakimovich
 */
@Controller
@RequestMapping(value = "/index")
public class UserController {
    
    private UserFormValidator validator;
 
    @Autowired
    public UserController(){
    }

    public UserFormValidator getValidator() {
        return validator;
    }

    public void setValidator(UserFormValidator validator) {
        this.validator = validator;
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView addUser(@ModelAttribute("command") User user,
                                   BindingResult result,
                                   SessionStatus status) {
 
        validator.validate(user, result);
 
        if(result.hasErrors()){
            return new ModelAndView("index", "command", user);
        }else {
            status.setComplete();
        }
 
        return new ModelAndView("index", "command", user);
    }
 
    @RequestMapping(method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        model.addAttribute("command", new User());
        return "index";
    }
}